import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PuppyCodeSWT implements Runnable {
	Display d;
	Shell shell;
	Canvas c;
	
	int alpha = 0;
	
	Point p1 = new Point(80 , 100);
	Point p2 = new Point(120 , 140);
	
	Image i1 , i2;
	public PuppyCodeSWT() {
		// TODO Auto-generated constructor stub
		new Thread(this).start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new PuppyCodeSWT();
	}

	public void run() {
		d = new Display();
		shell = new Shell(d);

		shell.setLayout(new FormLayout());
		
		i1 = new Image(d , "res/0.png");
		i2 = new Image(d , "res/1.png");
		
		FormData layoutData = null;

		layoutData = new FormData();

		layoutData.left = new FormAttachment(0, 0);
		layoutData.top = new FormAttachment(0, 0);
		layoutData.right = new FormAttachment(100, 0);
		layoutData.bottom = new FormAttachment(100, 0);
		

		c = new Canvas(shell , SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
		
		c.setLayoutData(layoutData);
		
		c.addPaintListener(new PaintListener(){

			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				final GC gc = e.gc;
				
				final Point size = c.getSize();
				int cw = size.x;
				int ch = size.y;
				
				gc.setBackground(d.getSystemColor(SWT.COLOR_WHITE));
				gc.fillRectangle(0 , 0 , cw , ch);
				
				final ImageData id1 = i1.getImageData();
				final ImageData id2 = i2.getImageData();
				
				int fw = id1.width + (id2.width - id1.width / 2);
				int fh = id1.height + (id2.height - id1.height / 2);
				
				gc.drawImage(i1, p1.x, p1.y);
				gc.drawImage(i2, p2.x, p2.y);
				
				Rectangle r1 = new Rectangle(p1.x , p1.y , id1.width , id1.height);
				Rectangle r2 = new Rectangle(p2.x , p2.y , id2.width , id2.height);
				
				Rectangle ir = r1.intersection(r2);
				
//				gc.setBackground(d.getSystemColor(SWT.COLOR_WHITE));
//				gc.fillRectangle(ir);

//				System.out.println(Integer.toHexString(id2.getPixel(0, 0)));
				
				for (int i = 0; i < ir.height; i++) {
					for (int j = 0; j < ir.width; j++) {
						int c1 = id1.getPixel(ir.x - p1.x + j, ir.y - p1.y + i);
						int c2 = id2.getPixel(j, i);
						
						int red1 = getRed(c1);
						int green1 = getGreen(c1);
						int blue1 = getBlue(c1);
						int a1 = id1.getAlpha(ir.x - p1.x + j, ir.y - p1.y + i);
						
						int red2 = getRed(c2);
						int green2 = getGreen(c2);
						int blue2 = getBlue(c2);
						int a2 = id2.getAlpha(j, i);
						
//						System.out.printf("%3d , %3d , %3d , %3d " , red2 , green2 , blue2 , a2);
//						System.out.printf("%3d " , a2);
						
						if(!(red2 == 255 && green2 == 255 && blue2 == 255) && a2 != 0){
//							System.out.println(i + "," + j);
//							int red = (int)(red1 * alpha + red2 * (1 - alpha));
//							int green = (int)(green1 * alpha + green2 * (1 - alpha));
//							int blue = (int)(blue1 * alpha + blue2 * (1 - alpha));
							
							int red = (int)(red1 * alpha / 100 + red2 * (100 - alpha) / 100);
							int green = (int)(green1 * alpha / 100+ green2 * (100 - alpha) / 100);
							int blue = (int)(blue1 * alpha / 100 + blue2 * (100 - alpha) / 100);
							
							Color nc = new Color(d , red , green , blue);
							
							gc.setBackground(nc);
							gc.fillRectangle(ir.x + j, ir.y + i, 1, 1);
							
							nc.dispose();							
						}
						
//						int red = (int)(red1 * alpha / 100 + red2 * (100 - alpha) / 100);
//						int green = (int)(green1 * alpha / 100+ green2 * (100 - alpha) / 100);
//						int blue = (int)(blue1 * alpha / 100 + blue2 * (100 - alpha) / 100);
//						
//						Color nc = new Color(d , red , green , blue);
//						
//						gc.setAlpha(255);
//						gc.setBackground(nc);
//						gc.fillRectangle(ir.x + j, ir.y + i, 1, 1);
//						
//						nc.dispose();				
					}
//					System.out.println();
				}
				
				gc.drawImage(i1, p1.x + 300, p1.y);
				gc.drawImage(i2, p1.x + 300 + p2.x - p1.x, p2.y);
			}
			
		});
		
		c.addMouseListener(new MouseAdapter(){
			
		});
		
		c.addMouseMoveListener(new MouseMoveListener(){

			public void mouseMove(MouseEvent e) {
				// TODO Auto-generated method stub
				c.redraw();
			}
			
		});
		
		c.addKeyListener(new KeyAdapter(){

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				
				int step = 5;
				
				if(e.keyCode == SWT.ARROW_UP){
					alpha += 5;
					alpha = Math.min(alpha, 100);
					c.redraw();
				}else if(e.keyCode == SWT.ARROW_DOWN){
					alpha -= step;
					alpha = Math.max(alpha, 0);
					c.redraw();
				}else if(e.keyCode == SWT.ARROW_LEFT){
					p2.x -= 1;
					c.redraw();
				}else if(e.keyCode == SWT.ARROW_RIGHT){
					p2.x += 1;
					c.redraw();
				}
			}
			
		});
		
		shell.layout();
		shell.open();

		// TODO Auto-generated method stub
		while (!shell.isDisposed()) {
			if (!d.readAndDispatch()) {
				d.sleep();
			}
		}
	}
	
	public int getAlpha(int c){
		return (c & 0xff000000) >>> 24;
	}
	
	public int getRed(int c){
		return (c & 0xff0000) >>> 16;
		
	}
	public int getGreen(int c){
		return (c & 0xff00) >>> 8;
	}
	public int getBlue(int c){
		return (c & 0xff);			
	}
	
}
